package me.lc.jokealot;

import me.lc.jokealot.entity.JokeBody;
import me.lc.jokealot.util.ShareSettingUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.admogo.AdMogoLayout;
import com.admogo.AdMogoListener;
import com.mobclick.android.MobclickAgent;

public class JokeActivity extends Activity implements AdMogoListener {
	TextView txtTitle;
	TextView txtBody;
	TextView txtCat;
	ScrollView scrollView;
	Button btnPrevious;
	Button btnNext;
	Button btnList;
	Button btnFontSize;
	Button btnShare;
	LinearLayout adLayout;

	String fontSizeKey = "fontSizeKey";

	// 本地变量
	int currentIndex;
	AlertDialog fontDialog;
	JokeBody currentBody;

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.joke_view);

		// 设置控件实例
		txtTitle = (TextView) findViewById(R.id.textTitle);
		txtBody = (TextView) findViewById(R.id.textBody);
		btnPrevious = (Button) findViewById(R.id.btnPrevious);
		btnNext = (Button) findViewById(R.id.btnNext);
		btnList = (Button) findViewById(R.id.btnList);
		btnFontSize = (Button) findViewById(R.id.btnFont);
		btnShare = (Button) findViewById(R.id.btnShare);
		txtCat = (TextView) findViewById(R.id.txtCategoryTitle);
		scrollView = (ScrollView) findViewById(R.id.scrollViewJokeView);
		adLayout = (LinearLayout) findViewById(R.id.admogo_layout);

		// 设置类型显示
		txtCat.setText(DataManager.instance.currentCategoryName);

		Intent intent = getIntent();
		int index = intent.getIntExtra("index", 0);

		setButtonClick();
		setJokeBody(index);

		setAD();

		// 读取字号信息并设置
		currentTextSize = ShareSettingUtil.getIntSetting(fontSizeKey, 20);
		tempTextSize = currentTextSize;
		setFontSize();
	}

	int currentTextSize = 20;
	int tempTextSize = 20;
	int minTextSize = 10;

	private void saveIndex() {
		Thread saveThread = new Thread() {
			@Override
			public void run() {
				DataManager.instance.saveLastJokeIndex(currentIndex);
			}
		};
		saveThread.start();
	}

	private void setButtonClick() {
		btnPrevious.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setJokeBody(currentIndex - 1);
				scrollView.smoothScrollTo(0, 0);
			}
		});

		btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setJokeBody(currentIndex + 1);
				scrollView.smoothScrollTo(0, 0);
			}
		});

		btnList.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(JokeActivity.this,
						CategoryActivity.class);
				JokeActivity.this.startActivity(intent);
				JokeActivity.this.finish();
			}
		});

		btnFontSize.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setFont();
			}
		});

		btnShare.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setShare();
			}
		});

		// TODO 广告审核时用
		// btnFontSize.setVisibility(View.INVISIBLE);
		// btnShare.setVisibility(View.INVISIBLE);
	}

	private void setShare() {
		// new AlertDialog.Builder(this)
		// .setTitle("分享笑话")
		// .setItems(new String[] { "通过短信", "复制笑话内容" },
		// new android.content.DialogInterface.OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface dialog,
		// int which) {
		// switch (which) {
		// case 0:
		// sendSms();
		// break;
		// case 1:
		// copyToClipBoard();
		// break;
		// }
		//
		// }
		// }).setNegativeButton("确定", null).show();

		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
		intent.putExtra(Intent.EXTRA_TEXT, currentBody.getJoke());
		startActivity(Intent.createChooser(intent, "分享笑话"));
	}

	private void sendSms() {
		Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
				"smsto", "", null));
		mmsintent.putExtra("sms_body", currentBody.getJoke());
		startActivity(mmsintent);
	}

	private void copyToClipBoard() {
		ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
		clipboard.setText(currentBody.getJoke());
		Toast.makeText(this, "已将笑话内容复制到剪贴板中", Toast.LENGTH_SHORT);
	}

	private void setFont() {
		// 弹出字体设置对话框
		SeekBar seekBar = new SeekBar(JokeActivity.this);
		seekBar.setMax(20);
		seekBar.setProgress(currentTextSize - minTextSize);
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				tempTextSize = progress + minTextSize;
				if (fontDialog != null) {
					fontDialog.setTitle("当前字号:" + tempTextSize);
				}
			}
		});
		fontDialog = new AlertDialog.Builder(JokeActivity.this)
				.setTitle("当前字号:" + currentTextSize)
				.setIcon(android.R.drawable.btn_star)
				.setView(seekBar)
				.setPositiveButton("确定",
						new android.content.DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								currentTextSize = tempTextSize;
								setFontSize();
								// 保存字号
								ShareSettingUtil.storeIntSetting(fontSizeKey,
										currentTextSize);
							}

						}).setNegativeButton("取消", null).show();
	}

	private void setFontSize() {
		txtTitle.setTextSize(currentTextSize * 1.25f);
		txtBody.setTextSize(currentTextSize);
	}

	private void setJokeBody(int index) {
		JokeBody body = DataManager.instance.loadJokeItem(
				DataManager.instance.currentCategoryIndex, index, this);
		if (body != null) {
			currentBody = body;
			currentIndex = index;
			txtTitle.setText(body.getTitle());
			txtBody.setText(body.getJoke());
			saveIndex();
		} else {
			Toast.makeText(this, "本分类最后一个笑话了", Toast.LENGTH_SHORT).show();
		}
	}

	// 广告是否已经显示
	private boolean isAdShowed = false;

	private synchronized void setAD() {
		// 是否出广告
		if (AppControl.isShowAd() && !isAdShowed) {
			isAdShowed = true;
			AdMogoLayout adMogoLayoutCode = new AdMogoLayout(this,
					"ec4927a1866b42799efd489b9a0e519f");
			adMogoLayoutCode.setAdMogoListener(this);
			FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.FILL_PARENT,
					FrameLayout.LayoutParams.WRAP_CONTENT);
			adLayout.addView(adMogoLayoutCode, params);
		}
	}

	@Override
	public void onClickAd() {
		// TODO lc 点击解锁

	}

	@Override
	public void onCloseAd() {

	}

	@Override
	public void onCloseMogoDialog() {

	}

	@Override
	public void onFailedReceiveAd() {

	}

	@Override
	public void onReceiveAd() {

	}
}
